package com.netease.examination.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * designed by destiny
 *
 * @author destiny
 *         e-mail destinywk@163.com
 *         github https://github.com/DestinyWang
 *         oschina https://git.oschina.net/destinywk
 * @version JDK 1.8.0_101
 * @since 2017/3/30 11:49
 */
@Component
public class LoginRequiredInterceptor implements HandlerInterceptor{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginRequiredInterceptor.class);

    /**
     * 核心方法
     *
     * 除了登录页之外，所有的页面均需要验证服务器下发的token
     * 采用拦截器实现
     *
     * @param httpServletRequest    Http请求
     * @param httpServletResponse   Http响应
     * @param o
     * @return                      当前用户是否已登录
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest,
                             HttpServletResponse httpServletResponse,
                             Object o)
            throws Exception {

        LOGGER.info("LoginRequiredInterceptor.preHandle()");

        // 获取session中的token
        HttpSession session = httpServletRequest.getSession();
        String tokenInSession = (String) session.getAttribute("token");

        if (tokenInSession == null || tokenInSession.length() == 0) {
            httpServletResponse.sendRedirect("/login");
            return false;
        }

        String tokenInCookie = null;

        // 获取cookie中的token
        for (Cookie cookie : httpServletRequest.getCookies()) {
            if ("token".equals(cookie.getName())) {
                tokenInCookie = cookie.getValue();
            }
        }

        if (tokenInCookie == null || tokenInCookie.length() == 0 ||
                !tokenInCookie.equals(tokenInSession)) {
            httpServletResponse.sendRedirect("/login");
            return false;
        }

        return true;
    }


    /**
     * 后置拦截
     * 此处为空实现
     * @param httpServletRequest    Http请求
     * @param httpServletResponse   Http响应
     * @param o
     * @param modelAndView          视图对象
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o,
                           ModelAndView modelAndView)
            throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o,
                                Exception e)
            throws Exception {

    }
}
